package l1j.server.server.datatables;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.model.drop.SetDrop;
import l1j.server.server.model.drop.SetDropExecutor;
import l1j.server.server.templates.L1Drop;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.PerformanceTimer;
import l1j.server.server.utils.SQLUtil;

public class DropTable {
	private static Logger _log = Logger.getLogger(DropTable.class.getName());
	private static DropTable _instance;

	public static DropTable getInstance() {
		if (_instance == null) {
			_instance = new DropTable();
		}
		return _instance;
	}
	
	
	private DropTable() {
		final PerformanceTimer timer = new PerformanceTimer();
		final Map<Integer, ArrayList<L1Drop>> droplists = this.allDropList();
		final SetDropExecutor setDropExecutor = new SetDrop();
		setDropExecutor.addDropMap(droplists);
		
		_log.info("載入物品掉落資料數量: " + droplists.size() + "(" + timer.get() + "ms)");
	}

	/**opqlo DB資料自動備註*/

	private HashMap<Integer, ArrayList<L1Drop>> allDropList() {
		final HashMap<Integer, ArrayList<L1Drop>> droplistMap = new HashMap<Integer, ArrayList<L1Drop>>();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("select * from droplist");

			rs = pstm.executeQuery();
			while (rs.next()) {
				final int itemId = rs.getInt("itemId");
				if (check_item(itemId)) {	
					final int mobId = rs.getInt("mobId");
					final int min = rs.getInt("min");
					final int max = rs.getInt("max");
					final int chance = rs.getInt("chance");
					final int bless = rs.getInt("bless");
					final int repeat = rs.getInt("迴圈");
					final L1Drop drop = new L1Drop(mobId, itemId, min, max, chance,repeat,bless);
					ArrayList<L1Drop> dropList = droplistMap.get(drop.getMobid());
					if (dropList == null) {
						dropList = new ArrayList<L1Drop>();
						droplistMap.put(new Integer(drop.getMobid()), dropList);
					}
					dropList.add(drop);
				}
			}
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return droplistMap;
	}


	private boolean check_item(int itemId) {
		final L1Item itemTemplate = ItemTable.getInstance().getTemplate(itemId);
		if (itemTemplate == null) {
			// 無該物品資料 移除
			errorItem(itemId);
			return false;
		}
		return true;
	}

	/**
	 * 刪除錯誤物品資料
	 * @param objid
	 */
	private static void errorItem(int itemid) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement(
					"DELETE FROM `droplist` WHERE `itemId`=?");
			pstm.setInt(1, itemid);
			pstm.execute();

		} catch (final SQLException e) {
			_log.log(Level.SEVERE,e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
//	private void ii(L1Drop drop){
//		Connection con = null;
//		PreparedStatement pstm = null;
//		final L1Item item = ItemTable.getInstance().getTemplate(drop.getItemid());
//		final L1Npc theMob = NpcTable.getInstance().getTemplate(drop.getMobid());
//		if(theMob == null){
//			_log.log(Level.WARNING , drop.getMobid()+"沒有這隻怪物。");
//			return;
//		}
//
//		if(item == null){
//			_log.log(Level.WARNING , drop.getItemid()+"沒有這個道具編號。");
//			return;
//		}
//
//		final String mobname = theMob.get_name() , itemname = item.getName();
//
//		try{
//			con = DatabaseFactory.get().getConnection();
//			pstm = con.prepareStatement("INSERT INTO droplist SET mobId=?, 怪物名稱=?, itemId=?, 道具名稱=?, min=? , max=? , chance=? , 迴圈=? ");
//			pstm.setInt(1, drop.getMobid());
//			pstm.setString(2, mobname);
//			pstm.setInt(3, drop.getItemid());
//			pstm.setString(4, itemname);
//			pstm.setInt(5, drop.getMin());
//			pstm.setInt(6, drop.getMax());
//			pstm.setInt(7, drop.getChance());
//			pstm.setInt(8, drop.getRepeat());
//
//
//			System.out.println("自動加註:怪物 [ "+mobname+" ] 掉落物品:[ "+itemname+" ]");
//			pstm.execute();
//
//		} catch (final SQLException e) {
//			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
//		} finally {
//			SQLUtil.close(pstm);
//			SQLUtil.close(con);
//		}
//	}

//	public void 掉落資料加載() throws SQLException{
//
//		if(Config.DB_UPDATE){/**opqlo DB資料自動備註*/
//			HashMap<Integer, ArrayList<L1Drop>> _droplists2 = this._droplists;
//			Connection con = null;
//			PreparedStatement pstm = null;
//			try {
//				con = DatabaseFactory.get().getConnection();
//				pstm = con.prepareStatement("DELETE FROM droplist");
//				pstm.execute();
//			} catch (final SQLException e) {
//				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
//			} finally {
//				SQLUtil.close(pstm);
//				SQLUtil.close(con);
//			}
//
//			for (final Integer id : _droplists2.keySet()) {
//				final ArrayList<L1Drop> droplist = _droplists2.get(id);
//				for(final L1Drop drop : droplist){
//					this.ii(drop);
//					try {
//						Thread.sleep(3);
//					} catch (final InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//			_droplists2 = null;
//		}
//	}
}